using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Core.Model;
using EzDesk.Core.ServiceImpl.Repositories.Interface;

namespace EzDesk.Core.ServiceImpl.Repositories
{
    public partial class OrganizationRepository : IOrganizationRepository
    {
        EzDeskDataContext _db;

        public OrganizationRepository(EzDeskDataContext db)
        {
            this._db = db;
        }
		
		#region IRepository<Organization> Members
		
        public Guid Add(Organization model, bool saveChanges)
        {   
            _db.ez_Organizations.InsertOnSubmit(
                new ez_Organization()
                {
					OrgId = model.Id,
					
					Title = model.Title,						
					Icon = model.Icon,						
                });
            if (saveChanges)
            {
                this.SaveChanges();
            }
            return model.Id;
        }

        public Organization GetById(Guid id)
        {
            Organization info = null;
            info = _db.ez_Organizations.Where(p => p.OrgId == id)
                .Select(p => new Organization()
                                    {
                                        Id = p.OrgId,
						
                                        Title = p.Title,						
                                        Icon = p.Icon,						
                                    })
                .SingleOrDefault();
            return info;
        }

        public IQueryable<Organization> GetAll()
        {
            return _db.ez_Organizations.Select(
                p => new Organization()
                {
					Id = p.OrgId,
					
					Title = p.Title,						
					Icon = p.Icon,						
                }
            );
        }

        public void Delete(Guid id, bool saveChanges)
        {
            ez_Organization entity = _db.ez_Organizations.Where(p => p.OrgId == id).SingleOrDefault();
            if (entity != null)
            {
                _db.ez_Organizations.DeleteOnSubmit(entity);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(Organization model, bool saveChanges)
        {
            var entity = _db.ez_Organizations.Where(p => p.OrgId == model.Id)
                .SingleOrDefault();

			entity.Title = model.Title;
			entity.Icon = model.Icon;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }
		#endregion
    }
}
